/**
 *
 */
package org.windowkit.toolkit.print;

import java.util.ArrayList;
import java.util.StringTokenizer;

import javax.print.attribute.standard.MediaSizeName;

/**
 * This class attempts to retrieve a named media and convert it to pleasent to
 * read string. Do not use this class to set any print attributes. Formatted
 * Strings produced:
 *
 * <pre>
 *      String: iso-a0 Formatted: ISO A0
 *      String: iso-a1 Formatted: ISO A1
 *      String: iso-a2 Formatted: ISO A2
 *      String: iso-a3 Formatted: ISO A3
 *      String: iso-a4 Formatted: ISO A4
 *      String: iso-a5 Formatted: ISO A5
 *      String: iso-a6 Formatted: ISO A6
 *      String: iso-a7 Formatted: ISO A7
 *      String: iso-a8 Formatted: ISO A8
 *      String: iso-a9 Formatted: ISO A9
 *      String: iso-a10 Formatted: ISO A10
 *      String: iso-b0 Formatted: ISO B0
 *      String: iso-b1 Formatted: ISO B1
 *      String: iso-b2 Formatted: ISO B2
 *      String: iso-b3 Formatted: ISO B3
 *      String: iso-b4 Formatted: ISO B4
 *      String: iso-b5 Formatted: ISO B5
 *      String: iso-b6 Formatted: ISO B6
 *      String: iso-b7 Formatted: ISO B7
 *      String: iso-b8 Formatted: ISO B8
 *      String: iso-b9 Formatted: ISO B9
 *      String: iso-b10 Formatted: ISO B10
 *      String: jis-b0 Formatted: JIS B0
 *      String: jis-b1 Formatted: JIS B1
 *      String: jis-b2 Formatted: JIS B2
 *      String: jis-b3 Formatted: JIS B3
 *      String: jis-b4 Formatted: JIS B4
 *      String: jis-b5 Formatted: JIS B5
 *      String: jis-b6 Formatted: JIS B6
 *      String: jis-b7 Formatted: JIS B7
 *      String: jis-b8 Formatted: JIS B8
 *      String: jis-b9 Formatted: JIS B9
 *      String: jis-b10 Formatted: JIS B10
 *      String: iso-c0 Formatted: ISO C0
 *      String: iso-c1 Formatted: ISO C1
 *      String: iso-c2 Formatted: ISO C2
 *      String: iso-c3 Formatted: ISO C3
 *      String: iso-c4 Formatted: ISO C4
 *      String: iso-c5 Formatted: ISO C5
 *      String: iso-c6 Formatted: ISO C6
 *      String: na-letter Formatted: NA Letter
 *      String: na-legal Formatted: NA Legal
 *      String: executive Formatted: Executive
 *      String: ledger Formatted: Ledger
 *      String: tabloid Formatted: Tabloid
 *      String: invoice Formatted: Invoice
 *      String: folio Formatted: Folio
 *      String: quarto Formatted: Quarto
 *      String: japanese-postcard Formatted: Japanese Postcard
 *      String: oufuko-postcard Formatted: Oufuko Postcard
 *      String: a Formatted: A
 *      String: b Formatted: B
 *      String: c Formatted: C
 *      String: d Formatted: D
 *      String: e Formatted: E
 *      String: iso-designated-long Formatted: ISO Designated Long
 *      String: italian-envelope Formatted: Italian Envelope
 *      String: monarch-envelope Formatted: Monarch Envelope
 *      String: personal-envelope Formatted: Personal Envelope
 *      String: na-number-9-envelope Formatted: NA Number 9 Envelope
 *      String: na-number-10-envelope Formatted: NA Number 10 Envelope
 *      String: na-number-11-envelope Formatted: NA Number 11 Envelope
 *      String: na-number-12-envelope Formatted: NA Number 12 Envelope
 *      String: na-number-14-envelope Formatted: NA Number 14 Envelope
 *      String: na-6x9-envelope Formatted: NA Envelope
 *      String: na-7x9-envelope Formatted: NA Envelope
 *      String: na-9x11-envelope Formatted: NA Envelope
 *      String: na-9x12-envelope Formatted: NA Envelope
 *      String: na-10x13-envelope Formatted: NA Envelope
 *      String: na-10x14-envelope Formatted: NA Envelope
 *      String: na-10x15-envelope Formatted: NA Envelope
 *      String: na-5x7 Formatted:
 *      String: na-8x10 Formatted:
 * </pre>
 *
 * @author Eric Lundin
 */
@SuppressWarnings("serial")
public class MediaNameDecoder extends MediaSizeName {

	/**
	 * The MediaSizeName attribute
	 *
	 * @param value
	 *            The MediaSizeName attribute.
	 */
	public MediaNameDecoder(final MediaSizeName media) {
		super(media.getValue());
	}

	/**
	 * Decodes the media name currently stored.
	 *
	 * @return A displayable string representing the media type.
	 */
	public String decodeMediaName() {
		String retVal = super.getStringTable()[getValue()];
		retVal = formatString(retVal);
		return retVal;
	}

	/**
	 * Debug method prints all known media types and their resultant format.
	 * Debug and developer informational use only.
	 */
	private void test() {
		for (String current : getStringTable()) {
			System.out.println("String: " + current + " Formatted: "
					+ formatString(current));
		}
	}

	/**
	 * Formats the given string from the string table to the displayable string
	 * name.
	 *
	 * @param s
	 *            The string to format.
	 * @return The formatted String.
	 */
	private String formatString(final String s) {
		String currentFormattedToken;
		StringTokenizer tokenizer = new StringTokenizer(s, "-");
		String formattedString = new String();
		ArrayList<String> formattedTokens = new ArrayList<String>();
		while (tokenizer.hasMoreTokens()) {
			currentFormattedToken = formatToken(tokenizer.nextToken());
			if (!currentFormattedToken.trim().equals("")) {
				formattedTokens.add(currentFormattedToken);
			}
		}

		formattedString = formattedTokens.get(0);
		for (int i = 1; i < formattedTokens.size(); i++) {
			formattedString += " " + formattedTokens.get(i);
		}
		if (formattedString.equals("NA")) {
			formattedString = "";
		}
		return formattedString.trim();
	}

	/**
	 * Formats one token of the total name.
	 *
	 * @param token
	 *            The current token.
	 * @return The formatted token.
	 */
	private String formatToken(final String token) {
		String retVal;
		if (token.equalsIgnoreCase("ISO") || token.equalsIgnoreCase("JIS")
				|| token.equalsIgnoreCase("NA")
				|| token.matches("[a-zA-z][0-9]")) {
			retVal = token.toUpperCase();
		} else if (token.matches("[0-9]*[x][0-9]*")) {
			retVal = "";
		} else {
			if (token != null && !token.trim().equals("")) {
				retVal = token.substring(0, 1).toUpperCase()
						+ token.substring(1);
			}
			else {
				retVal = "";
			}
		}
		return retVal;

	}

}
